package com.sf.hackathons.app.ui.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.github.yoojia.qrcode.qrcode.QRCodeEncoder;
import com.sf.hackathons.app.R;
import com.sf.hackathons.app.presenter.constract.QRCodeConstract;
import com.sf.hackathons.app.presenter.impl.QRCodePresenter;
import com.sf.hackathons.lib.bean.UserInfo;
import com.sf.hackathons.lib.commui.base.BaseActivity;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnLongClick;

/**
 * Created by 01367478 on 2017/12/15.
 */

public class QRCodeActivity extends BaseActivity<QRCodeConstract.View, QRCodeConstract.Presenter> implements QRCodeConstract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_kg)
    TextView tvKg;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.image_qrcode)
    ImageView imageQrCode;

    private int mGroupId;
    private String mGroupName;
    private double weightMax;
    private double weightMin;

    private Bitmap bitmap;

    @OnLongClick(R.id.image_qrcode)
    boolean onLongClick() {
        saveBitmap(bitmap);
        return true;
    }

    @OnClick(R.id.btn_share)
    void onclick() {

    }

    @Override
    protected int initContentView() {
        return R.layout.activity_qrcode;
    }

    @Override
    protected void initData() {
        if (this.getIntent() == null) {
            return;
        }
        mGroupId = this.getIntent().getIntExtra("groupId", 1);
        mGroupName = this.getIntent().getStringExtra("groupName");
        weightMax = this.getIntent().getDoubleExtra("weightMax", 0);
        weightMin = this.getIntent().getDoubleExtra("weightMin", 0);
        tvTitle.setText(mGroupName);
        tvKg.setText(weightMax + "~" + weightMin + "kg");
        tvNumber.setText(mGroupId + "");
        generateCode();
    }

    public void saveBitmap(Bitmap bitmap) {
        if (bitmap == null) {
            return;
        }
        String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
        File f = new File(filePath, System.currentTimeMillis() + ".png");
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
            Toast.makeText(this, "图片已保存", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            Log.d("TAG", "FileNotFoundException", e);
        } catch (IOException e) {
            Log.d("TAG", "IOException", e);
        }
    }

    private void generateCode() {
        Bitmap centerImage = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_logo);
        Bitmap qrCodeImage = new QRCodeEncoder.Builder()
                .width(480) // 二维码图案的宽度
                .height(480)
                .paddingPx(0) // 二维码的内边距
                .marginPt(3) // 二维码的外边距
                .centerImage(centerImage) // 二维码中间图标
                .build()
                .encode(String.valueOf(UserInfo.userId));

        imageQrCode.setImageBitmap(qrCodeImage);
        this.bitmap = qrCodeImage;
    }

    @Override
    protected void initProcess(Bundle savedInstanceState) {

    }

    @NonNull
    @Override
    public QRCodeConstract.Presenter createPresenter() {
        return new QRCodePresenter();
    }

}
